package me.pinion.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import me.pinion.model.annotation.TableBind;

import java.util.List;



@TableBind(table = "tag")
public class Tag extends XModel<Tag>{
    public static Tag dao = new Tag();

    public String getName(){
        return getStr("name");
    }

    public Tag put(Object targetId, Class<? extends XModel> type){
        ModelType modelType = ModelType.dao.getByClass(type);
        Record br = new Record();
        br.set("tag_id", this.getId());
        br.set("target_id", targetId);
        br.set("target_type_id", modelType.getId());
        Db.save("tag_object", br);
        return this;
    }

    public Tag put(XModel model){
        return this.put(model.getInt("id").longValue(), model.getClass());
    }

    public Tag findByName(String tag){
        if (tag == null){
            return null;
        }
        List<Tag> tags = dao.find("SELECT * FROM tag WHERE tag.name=?", tag);
        if(tags.size() == 0){
            tags.add(dao.create(tag));
        }
        return tags.get(0);
    }

    public Tag create(String name){
        Tag tag = new Tag();
        tag.set("name", name);
        tag.save();
        return tag;
    }


    public Tag top(Model<? extends XModel> model){
        ModelType modelType = ModelType.dao.getByClass((Class<? extends XModel>) model.getClass());
        return Tag.dao.findFirst("SELECT t.* FROM tag AS t, tag_object AS tob WHERE t.id=tob.tag_id AND tob.target_id=? AND tob.target_type_id=?  GROUP BY t.id ORDER BY count(t.id) DESC", model.getInt("id"), modelType.getInt("id"));
    }

    public List<Tag> top(Model<? extends XModel> model, int max){
        ModelType modelType = ModelType.dao.getByClass((Class<? extends XModel>) model.getClass());
        return Tag.dao.paginate(1, max, "SELECT t.*, COUNT(t.id)",
                "FROM tag AS t, tag_object AS tob WHERE t.id=tob.tag_id AND tob.target_id=? AND tob.target_type_id=? GROUP BY t.id ORDER BY count(t.name) DESC",
                model.getInt("id"),
                modelType.getInt("id")).getList();
    }

    public List<Tag> top(Class<? extends XModel> clazz, Object id,int max){
        try {
            return this.top(clazz.newInstance().findById(id), max);
        } catch (InstantiationException e) {
            throw new RuntimeException();
        } catch (IllegalAccessException e) {
            throw new RuntimeException();
        }
    }

    public List<Tag> top(String clazz, Object id,int max){
        try {
            return this.top((Class<? extends XModel>) Class.forName(clazz), id, max);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException();
        }
    }

    public List<Tag> top3(Model<? extends XModel> model){
        return this.top(model, 3);
    }

    public List<Tag> top5(Model<? extends XModel> model){
        return this.top(model, 5);
    }
}
